package main

import (
	"fmt"
	"gitee.com/sansaniot/ssiot-gw-lib/device"
	"gitee.com/sansaniot/ssiot-gw-lib/gateway"
	"gitee.com/sansaniot/ssiot-gw-lib/ssiot"
	"os"
	"os/signal"
	"syscall"
)

type GwConfig struct {
	Conn struct {
		Dsn string `json:"dsn" yaml:"dsn"`
	} `json:"conn" yaml:"conn"`
	Syn string `json:"syn" yaml:"syn"`
}

var gwConfig GwConfig

func deviceConfigSet(sid uint64, devSn string, config interface{}) error {
	fmt.Println("dev config set....")

	return nil
}

func deviceSensorSet(sid uint64, devSn string, config interface{}) error {
	fmt.Println("dev sensor set....")

	return nil
}

func main() {
	// 网关连接到平台，将同步平台的基础配置（ssiot节点下），并与本地配置合并，再同步到平台
	ssiot.Init()

	// 注册网关配置
	gateway.RegisterGatewayConfig(&gwConfig)

	// 网关初始化，将同步平台的网关配置 （gateway节点下），并与本地配置合并，再同步到平台
	gateway.Init()

	fmt.Printf("gateway syn config is %v \n", gwConfig.Syn)

	// 设备config设置回调
	device.RegisterConfigSetHandler(deviceConfigSet)
	// 设备传感量设置回调
	device.RegisterSensorSetHandler(deviceSensorSet)

	// 写入配置，将同步写入配置文件，并同步到平台
	gwConfig.Conn.Dsn = "test write"
	_ = gateway.SaveGatewayConfig(&gwConfig)

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
	<-sigc

	ssiot.Fini()
	println("exit OK")
}
